
const {login} = require('../controller/user')
const {successModel, errorModel} = require('../model/resModel')
//const { get, set } = require('../data/redis')
const {exec} = require('../data/mysql')
//设置cookie的过期时间
const cookieExpires = () => {
    const date = new Date()
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000))
    // console.log(date.toGMTString())
    return date.toGMTString()
}

const handleUserRouter = (req, res) => {
    //获取请求的一些信息
    const method = req.method
     
    //接口
    //1、登陆接口
    if(method === 'POST' && req.path === '/api/user/login') {
       // console.log(req.path)
        const {username,password} = req.body
        //console.log(username,password)
       // const {username,password} = req.query
        const result = login(username,password)
        return result.then(data => {
             //console.log(data.username)
            if(data.username) {
                //设置session
                req.session.username = data.username
                req.session.realname = data.realname
                //sql处理
                const sql = `update session set username='${data.username}',realname='${data.realname}' where sessionId='${req.sessionId}';`
                exec(sql).then(val => {
                    // console.log(val)
                }) 
                return new successModel('登陆成功')
                //redis处理
                //  set(req.sessionId, req.session)  
                
            }
                return new errorModel('登陆失败')
            
        })
    }
    
    // //登陆验证：cookie中有username就说明登陆了
    // if(method === 'GET' && req.path === '/api/user/login-test') {
    //     if(req.session.username) {
    //         return Promise.resolve(new successModel({
    //             username:req.session
    //         }))
    //     }
    //     return Promise.resolve(new errorModel('没有登陆'))
    // }



    //2、获取博客详情
    if(method === 'GET' && req.path === '/api/blog/detail') {
        return {
            meg: '博客详情的接口'
        }
    }

    //3、新建博客
    if(method === 'POST' && req.path === '/api/blog/new') {
        return {
            meg: '新建博客的接口'
        }
    }
    
    //4、更新博客
    if(method === 'POST' && req.path === '/api/blog/update') {
        return {
            meg: '更新博客的接口'
        }
    }


    //5、删除博客
    if(method === 'POST' && req.path === '/api/blog/del') {
        return {
            meg: '删除博客的接口'
        }
    }

}

module.exports = handleUserRouter